package com.borya.dao.impl;

import com.borya.dao.RegistOrderInfoDAO;
import com.borya.framework.annotation.Component;
import com.borya.framework.core.ServerSqlSession;
import com.borya.model.db.RegistOrderInfo;
import org.apache.log4j.Logger;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Component("registOrderInfoDAO")
public class RegistOrderInfoDAOImpl extends BaseDAOImpl implements RegistOrderInfoDAO{
	
	
	private Logger log = Logger.getLogger(getClass());
	
	@Override
	public RegistOrderInfo getToAuditOrderInfo(String orderId) {
		try (ServerSqlSession session = getSqlDataSource().session()){
			StringBuilder sb = new StringBuilder(256);
            sb.append("select sdk_user_id,operation_type,id,order_id,request_phone,devMac,before_status,company_name,address");
            sb.append(",audit_time,order_status,customer,user_id,dealer_id,period,merchant_type,detail_type");
			sb.append(",head_picture,source_type,user_name,store_address,sign_picture");
			sb.append(",door_picture_left,door_picture_right,hand_picture,business_licence");
			sb.append(",city_code,city_name,period");
			sb.append(" from tb_registor_audit ");
			sb.append(" where order_id='").append(orderId).append("'");
			sb.append(" and order_status=5");
			sb.append(" limit 1");
			return session.executeQuery(sb.toString(), rs-> {
				return castEntity(rs);
			});
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return null;
		}
	}

	private RegistOrderInfo castEntity(ResultSet rs)throws SQLException{
		if(rs != null && rs.next()){
			RegistOrderInfo r = new RegistOrderInfo();
			r.setId(rs.getInt("id"));
			r.setOrderId(rs.getString("order_id"));
			r.setRequestPhone(rs.getString("request_phone"));
			r.setDevMac(rs.getString("devMac"));
			r.setBeforeStatus(rs.getInt("before_status"));
			r.setAuditTime(rs.getLong("audit_time"));
			r.setOrderStatus(rs.getInt("order_status"));
			r.setCustomer(rs.getString("customer"));
			r.setUserId(rs.getString("user_id"));
			r.setDealerId(rs.getString("dealer_id"));
			r.setPeriod(rs.getString("period"));
			r.setHandPicture(rs.getString("head_picture"));
			r.setSourceType(rs.getInt("source_type"));
			r.setCompanyName(rs.getString("company_name"));
			r.setAddress(rs.getString("address"));
			r.setUserName(rs.getString("user_name"));
			r.setStoreAddress(rs.getString("store_address"));
			r.setSignPicture(rs.getString("sign_picture"));
			r.setDoorPictureLeft(rs.getString("door_picture_left"));
			r.setDoorPictureRight(rs.getString("door_picture_right"));
			r.setHandPicture(rs.getString("hand_picture"));
			r.setBusinessLicence(rs.getString("business_licence"));
			r.setCityCode(rs.getString("city_code"));
			r.setCityName(rs.getString("city_name"));
			r.setMerchantType(rs.getInt("merchant_type"));
			r.setPeriod(rs.getString("period"));
            r.setSdkUserId(rs.getString("sdk_user_id"));
            r.setOperationType(rs.getString("operation_type"));
            r.setDetailType(rs.getInt("detail_type"));
			return r;
		}
		return null;
	}
	
	@Override
	public RegistOrderInfo getLastByUserId(String userId) {
		StringBuilder sb = new StringBuilder(256);
        sb.append("select sdk_user_id,operation_type,id,order_id,request_phone,devMac,before_status,company_name,address");
        sb.append(",audit_time,order_status,customer,user_id,dealer_id,period,merchant_type,detail_type");
		sb.append(",head_picture,source_type,user_name,store_address,sign_picture");
		sb.append(",door_picture_left,door_picture_right,hand_picture,business_licence");
		sb.append(",city_code,city_name,period");
		sb.append(" from tb_registor_audit ");
		sb.append(" where user_id='").append(userId).append("'");
		sb.append(" order by id desc ");
		sb.append(" limit 1");
		
//		json.put("period", orderInfo.getPeriod());
		try (ServerSqlSession session = getSqlDataSource().session()){
			return session.executeQuery(sb.toString(), rs-> {
				return castEntity(rs);
			});
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return null;
		}
	}

    @Override
    public List<RegistOrderInfo> getRegistorAuditListByIdCard(String idCard, Integer detailType, Integer type) {
        try (ServerSqlSession session = getSqlDataSource().session()) {
            StringBuilder sql = new StringBuilder(256);
            sql.append("select * from tb_registor_audit ");
            sql.append(" where audit_type = 1 and detail_type = ? and order_status IN (3,4) ");
            if (1 == type) {
                sql.append(" and identityCard = ? ");
            } else {
                sql.append(" and business_licence = ? ");
            }
            return session.executeQuery(sql.toString(), rs -> {
                List<RegistOrderInfo> list = new ArrayList<>();
                if (rs != null) {
                    while (rs.next()) {
                        RegistOrderInfo info = new RegistOrderInfo();
                        info.setOrderId(rs.getString("order_id"));
                        list.add(info);
                    }
                }
                return list;
            }, detailType, idCard);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    @Override
    public RegistOrderInfo getRegistorAuditListBySdkUserId(String sdkUserId, Integer detailType) {
        try (ServerSqlSession session = getSqlDataSource().session()) {
            StringBuilder sql = new StringBuilder(256);
            sql.append("select * from tb_registor_audit ");
            sql.append(" where detail_type = ? and order_status IN (1,3,4,) ");
            sql.append(" and sdk_user_id = ? ");
            return session.executeQuery(sql.toString(), rs -> {
                if (rs != null && rs.next()) {
                    RegistOrderInfo info = new RegistOrderInfo();
                    info.setOrderId(rs.getString("order_id"));
                    info.setOrderStatus(rs.getInt("order_status"));
                    return info;
                }
                return null;
            }, detailType, sdkUserId);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    @Override
    public List<RegistOrderInfo> getRegistorAuditListByUserId(String sdkUserId, Integer detailType) {
        try (ServerSqlSession session = getSqlDataSource().session()) {
            StringBuilder sql = new StringBuilder(256);
            sql.append("select * from tb_registor_audit ");
            sql.append(" where sdk_user_id = ? and detail_type = ? and order_status IN (3,4,5) ");
            return session.executeQuery(sql.toString(), rs -> {
                List<RegistOrderInfo> list = new ArrayList<>();
                if (rs != null) {
                    while (rs.next()) {
                        RegistOrderInfo info = new RegistOrderInfo();
                        info.setOrderId(rs.getString("order_id"));
                        info.setOrderId(rs.getString("user_id"));
                        list.add(info);
                    }
                }
                return list;
            }, sdkUserId, detailType);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

}